define([
    'jquery',
    'underscore',
    'backbone',
    //'text!templates/form/form_add_admin.html'
    ], function($, _, Backbone,loginTemplate){
        var $html_loadding = '<div class="caculation-report"><img style="margin-left:170px" src="'+$template_admincp_url+'img/standard/loaders/loading64.gif"/></div>';
        var articleView = Backbone.View.extend({
            el: $("#main"),
            initialize: function(){
				
            },
            events: {
                
            },
            
            index: function(){
                $(document).on( "click", "a.active", function(){
                    var temp;
                    var active = 'i-checkmark';
                    var deactive = 'i-close';
                    var status = $(this).find('i').attr('class');
                    var dataStatus = status.split(" ");
                    var id = $(this).attr('art_id');
                    $.ajax({
                        url: $admincp_url + 'article/check',
                        type: 'post',
                        data: 'status='+dataStatus[1]+'&id='+id+'&active='+active+'&deactive='+deactive,
                        async: false,
                        success: function(html){
                            temp = html;
                        }
                        
                    });
                    $(this).html('<i class="icon16 '+temp+'">');
                    
                });
                var url = location.href;
                if(typeof oTable != "undefined"){
                    $("#table_article").dataTable().fnDestroy();
                }
                oTable = $('#table_article').dataTable({
                    "sDom": "<'row'<'col-lg-8'l><'col-lg-1 button_create'><'col-lg-3'f>r>t<'row'<'col-lg-6'i><'col-lg-6'p>>",
                    "sPaginationType": "bootstrap",
                    "bJQueryUI": false,
                    "bAutoWidth": false,
                    "oLanguage": {
                        "sUrl": $base_url+"public/templates/admincp/js/language/datatable.vn.txt"
                    },
                    "sScrollX": "100%",
                    //"sScrollXInner": "100%",
                    "bScrollCollapse": true,
                    "iDisplayLength": 10,
                    "iDisplayStart": 0,
                    "bProcessing": true,
                    "bRetrieve": true,
                    "aaSorting": [],
                    "bServerSide": true,
                    "sAjaxSource": url,
                    "fnServerData": function(sSource, aoData, fnCallback, oSettings) {
                        if(typeof filters != "undefined"){
                            obj_tmp = new Object;
                            $.each(filters, function(name, value){
                                obj_tmp[name] = value;
                            });
                            aoData.push({
                                name: "myFilter",
                                value: JSON.stringify(obj_tmp)
                            });
                        }
                        $.ajax( {
                            "dataType": 'json',
                            "type": "POST",
                            "url": sSource,
                            "data": aoData,
                            success: function(rs){
                                ids = rs.my_id;
                                fnCallback(rs);
                            }
                        });

                    },
                    "aoColumns": [
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "sec_name"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "cata_name"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "art_title"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "art_author"
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "string",
                            "swidth": "25%",
                            "sName": "art_check",
                            "sClass": "icon_table",
                            "bSortable": false
                        },
                        {
                            // "mData": "admin_name",
                            "sType": "date",
                            "swidth": "25%",
                            "sClass": "string",
                            "sName": "date_add"
                        },
                        {
                            // "mData": "action",
                            "sType": "string",
                            "swidth": "3%",
                            "sClass": "center",
                            "bSortable": false
                        }
                    ],

                    fnInitComplete: function()
                    {
                        $(this).slideDown(200);
                        $(".button_create").html('<button class="btn btn-primary btn-create" type="button">Create New</button>');
                        $(".btn-create").click(function(){
                            window.location.href = $admincp_url+'article/add';
                        });
                    }
                });
                $('.dataTables_length select').uniform();
                $('.dataTables_paginate > ul').addClass('pagination');
            },

            add: function(){
                tinymce.init({
                    selector: "textarea.art_content",
                    theme: "modern",
                    fontsize_formats: "8pt 9pt 10pt 11pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 21pt 22pt 23pt 24pt 25pt 26pt 36pt",
                    plugins: [
                        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                        "searchreplace visualblocks visualchars code fullscreen",
                        "insertdatetime media nonbreaking save table contextmenu directionality",
                        "emoticons template paste textcolor jbimages"
                    ],
                    toolbar1: "insertfile undo redo | styleselect | fontselect | fontsizeselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages",
                    toolbar2: "print preview media | forecolor backcolor emoticons",
                    image_advtab: true,
                    relative_urls: false,
                    height: 300,
                    templates: [
                        {title: 'Test template 1', content: 'Test 1'},
                        {title: 'Test template 2', content: 'Test 2'}
                    ]
                });
                // Form Validate
                $('#section').select2({placeholder: "Select Section"});
                $('#catalogue').select2({placeholder: "Select Catalogue"});
                $('.gallery').select2({placeholder: "Select Gallery"});
                $("#form").validate({
                    ignore: null,
                    ignore: 'input[type="hidden"]',
                    rules: {
                        section: "required",
                        catalogue: "required",
                        agree: "required"
                    },
                    submitHandler: function(form){
                        form.submit();
                    }
                });
                $("#section").change(function(){
                    var sec_id = $(this).val();
                    $.ajax({
                        url: $base_url+'admincp/article/process',
                        type: 'post',
                        data: 'sec_id='+sec_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#catalogue").html(rs.html);
                        }
                    });
                });
                $(".addMore").click(function(){
                    var parrent = $(this).closest('.tab-pane');
                    var lang = $(this).attr('lang');
                    $.ajax({
                        url: $base_url+'admincp/article/getMore',
                        type: 'post',
                        data: 'lang='+lang,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            parrent.find(".subArticle").append(rs.html);
                            parrent.find(".subArticle").find('select').select2({placeholder: "Select Gallery"});
                            $(".deleteSub").click(function(){
                                articleView.deleteSub($(this),0);
                            });
                        }
                    }); 
                });
                $(".deleteSub").click(function(){
                    articleView.deleteSub($(this),0);
                });
            },

            deleteSub: function($object, $id){
                var $this = $object;
                // var id = $this.attr('ids');
                if($id != 0){
                    $.ajax({
                        url: $base_url+'admincp/article/deleteSub',
                        type: 'post',
                        data: 'id='+id,
                        async: false,
                        success: function(){
                            $this.closest('.form-group').remove();
                        }
                    });
                }else{
                    $this.closest('.form-group').remove();
                }
            },

            edit: function(){
                tinymce.init({
                    selector: "textarea.art_content",
                    fontsize_formats: "8pt 9pt 10pt 11pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 21pt 22pt 23pt 24pt 25pt 26pt 36pt",
                    plugins: [
                        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                        "searchreplace visualblocks visualchars code fullscreen",
                        "insertdatetime media nonbreaking save table contextmenu directionality",
                        "emoticons template paste textcolor jbimages"
                    ],
                    toolbar1: "insertfile undo redo | styleselect | fontselect | fontsizeselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages",
                    toolbar2: "print preview media | forecolor backcolor emoticons",
                    image_advtab: true,
                    relative_urls: false,
                    height: 300,
                    templates: [
                        {title: 'Test template 1', content: 'Test 1'},
                        {title: 'Test template 2', content: 'Test 2'}
                    ]
                });
                // Form Validate
                $('#section').select2({placeholder: "Select Section"});
                // $('#catalogue').select2({placeholder: "Select Catalogue"});
                $('.gallery').select2({placeholder: "Select Gallery"});
                $("#form").validate({
                    ignore: null,
                    ignore: 'input[type="hidden"]',
                    rules: {
                        section: "required",
                        catalogue: "required",
                        agree: "required"
                    },
                    submitHandler: function(form){
                        form.submit();
                    }
                });
                var sec_id = $('#section').val();
                var cata_id = $('#catalogueHide').val();
                if(sec_id != 0){
                    $.ajax({
                        url: $base_url+'admincp/article/process',
                        type: 'post',
                        data: 'sec_id='+sec_id+'&cata_id='+cata_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#catalogue").html(rs.html);
                        }
                    });
                }else{
                    $("#catalogue").find("option").remove();
                    $("#catalogue").append("<option value='0'>Not Data</option>");
                }
                $("#section").change(function(){
                    var sec_id = $(this).val();
                    $.ajax({
                        url: $base_url+'admincp/article/process',
                        type: 'post',
                        data: 'sec_id='+sec_id,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            $("#catalogue").html(rs.html);
                        }
                    });
                });
                $(".addMore").click(function(){
                    var parrent = $(this).closest('.tab-pane');
                    var lang = $(this).attr('lang');
                    $.ajax({
                        url: $base_url+'admincp/article/getMore',
                        type: 'post',
                        data: 'lang='+lang,
                        async: false,
                        success: function(rs){
                            rs = JSON.parse(rs);
                            parrent.find(".subArticle").append(rs.html);
                            parrent.find(".subArticle").find('select').select2({placeholder: "Select Gallery"});
                            $(".deleteSub").click(function(){
                                articleView.deleteSub($(this),0);
                            });
                        }
                    }); 
                });
                $(".deleteSub").click(function(){
                    var id = $(this).attr('ids');
                    articleView.deleteSub($(this),id);
                });
                // $(".deleteSub").click(function(){
                //     var $this = $(this);
                    
                //     if(id.length != 0){
                //         $.ajax({
                //             url: $base_url+'admincp/article/deleteSub',
                //             type: 'post',
                //             data: 'id='+id,
                //             async: false,
                //             success: function(){
                //                 $this.closest('.form-group').remove();
                //             }
                //         });
                //     }else{
                //         $this.closest('.form-group').remove();
                //     }
                // });
            },
            
            render: function(){
                if(typeof this[$app.action] != 'undefined'){
                    new this[$app.action];
                }
            }
        });
        return articleView = new articleView;
    });
